<?php
require_once("../../config.php");
require_once(CONSTANT."alltype.php");
require_once(USERDIR."isuser.php");
//$_SESSION['username']="qqq123";
//$_SESSION['userid']="2";
$uid=new UID();
if(isset($_GET['s']))  //同上一张图片属性处理
{
	$productid=$dsql->getone("select productid from pic where userid = ".$_SESSION['userid']." order by productid desc limit 1");
	dopic($productid);

}
elseif(!empty($_POST['category']))  //新图片属性处理
{
	$postarray=$_POST;
	$sencondary="type_".$_POST['category'];
	foreach($type_product as $key => $value)
	{
		$inproduct[$key]=$postarray[$key];
	}
	foreach($$sencondary as $key => $value)
	{
		$insecond[$key]=$postarray[$key];
	}
	//var_dump($insecond);
	$uid->insert("product",$inproduct);
	$productid=$dsql->GetLastID();
	$insecond['productid']=$productid;
	$uid->insert($_POST['category'],$insecond);
	dopic($productid);

}


header("location:.");

function dopic($productid)
{
	global $uid;
	$picname=check_picname($_POST['picname']);
	//echo basename((PICDIR.$_SESSION['username']."/".$picname),".jpeg");
	//var_dump($inproduct);
	$inpic=array("file"=>$picname,"productid"=>$productid,"userid"=>$_SESSION['userid']);
	//var_dump($inpic);
	$uid->insert("pic",$inpic);
	rename(PICDIR.$_SESSION['username']."/".$_POST['picname'],PICDIR.$_SESSION['username']."/done/".$picname);
}

function check_picname($picname)
{
	global $dsql;
	$rs=$dsql->getone("select id from pic where file = '$picname'");
	if(!$rs)										
		return $picname;
	else
	{	
		$length=18;
		$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
		$password="";
		$picnames=explode(".", $picname);
		$pictype=$picnames[count($picnames)-1];
		for ( $i = 0; $i < $length; $i++ )
		{
        // 这里提供两种字符获取方式
        // 第一种是使用 substr 截取$chars中的任意一位字符；
        // 第二种是取字符数组 $chars 的任意元素
         $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        //$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
		}
		
		$newfilename=$password.".".$pictype;
		return check_picname($newfilename);
	}
}
?>